package com.ormbench.ebean;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.EbeanServerFactory;
import com.avaje.ebean.AdminLogging.LogLevel;
import com.avaje.ebean.config.DataSourceConfig;
import com.avaje.ebean.config.ServerConfig;
import com.ormbench.base.dao.AccountDAO;
import com.ormbench.base.dao.BranchDAO;
import com.ormbench.base.dao.DAOFactory;
import com.ormbench.base.dao.HistoryDAO;
import com.ormbench.base.dao.TellerDAO;
import com.ormbench.base.model.Account;
import com.ormbench.base.model.BaseModel;
import com.ormbench.base.model.Branch;
import com.ormbench.base.model.History;
import com.ormbench.base.model.Teller;

public class EbeanDAOFactory implements DAOFactory {
	
	public void configure(String driver, String url, String username, String password) throws Exception {
		ServerConfig cfg = new ServerConfig();  
		cfg.setName("ormbench");  
		cfg.setDefaultServer(true);
		cfg.setLoggingLevel(LogLevel.NONE);
		cfg.addClass(Account.class);
		cfg.addClass(BaseModel.class);
	    cfg.addClass(Branch.class);
	    cfg.addClass(History.class);
	    cfg.addClass(Teller.class);
		  
		// Define DataSource parameters  
		DataSourceConfig dsConfig = new DataSourceConfig();
		dsConfig.setDriver(driver);  
		dsConfig.setUrl(url);
		dsConfig.setUsername(username);  
		dsConfig.setPassword(password);
		cfg.setDataSourceConfig(dsConfig);  
		  
		// create the EbeanServer instance  
		EbeanServerFactory.create(cfg);
	}

	public void beginTransaction() {
		Ebean.beginTransaction();
	}

	public void commitTransaction() {
		Ebean.commitTransaction();
	}

	public BranchDAO getBranchDAO() {
		return EbeanBranchDAO.getInstance();
	}
	
	public TellerDAO getTellerDAO() {
		return EbeanTellerDAO.getInstance();
	}
	
	public AccountDAO getAccountDAO() {
		return EbeanAccountDAO.getInstance();
	}

	public HistoryDAO getHistoryDAO() {
		return EbeanHistoryDAO.getInstance();
	}
}
